using MediatR;
using UniversalAdmin.Application.Dtos.Log;
using UniversalAdmin.Application.Services.Interfaces;
using UniversalAdmin.Domain.Entities.App;

namespace UniversalAdmin.Application.Services.Handlers;

public class UserDeletedEventHandler : INotificationHandler<UserDeletedEvent>
{
    private readonly ILogService _logService;
    private readonly IUserContextService _userContextService;

    public UserDeletedEventHandler(ILogService logService, IUserContextService userContextService)
    {
        _logService = logService;
        _userContextService = userContextService;
    }

    public async Task Handle(UserDeletedEvent notification, CancellationToken cancellationToken)
    {
        // 创建日志对象
        var createLogDto = new CreateLogDto
        {
            UserId = _userContextService.GetCurrentUserId() ?? notification.User.Id, // 使用当前操作者ID
            Action = "UserDeleted",       // 日志动作类型
            Resource = notification.User.Username,            // 资源类型
            ResourceId = notification.User.Id
        };

        // 调用日志服务记录日志
        await _logService.AddAsync(createLogDto);

        // 这里只做简单日志输出，实际可扩展为发送邮件、写日志等
        Console.WriteLine($"用户删除事件触发，用户名：{notification.User.Username}，用户ID：{notification.User.Id}");
        
        // 可以在这里添加其他业务逻辑，比如：
        // 1. 发送邮件通知管理员
        // 2. 清理用户相关的缓存
        // 3. 记录审计日志
        // 4. 触发其他相关业务事件
    }
} 